'''
Created on Aug 24, 2011

@author: bstar
'''
from mongoalchemy.query import Query

# The MIT License
# 
# Copyright (c) 2010 Jeffrey Jenkins
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from mongoalchemy.query import Query, QueryResult

class txQueryResult(QueryResult):
    def __init__(self, cursor, type, raw_output=False, fields=None):
        print "========================"
        print cursor
        print "========================"
        self.cursor = iter(cursor)       
        self.type = type
        self.fields = fields
        self.raw_output = raw_output
    
    def next(self):
        value = self.cursor.next()
        if not self.raw_output:
            value = self.type.unwrap(value, fields=self.fields)
        return value
    
    def __getitem__(self, index):
        value = self.cursor.__getitem__(index)
        if not self.raw_output:
            value = self.type.unwrap(value)
        return value
    
    def rewind(self):
        return self.cursor.rewind()
    
    def clone(self):
        return QueryResult(self.cursor.clone(), self.type, 
            raw_output=self.raw_output, fields=self.fields)
    
    def __iter__(self):
        return self

class txQuery(Query):
    ''' A query object has all of the methods necessary to programmatically 
        generate a mongo query as well as methods to retrieve results of the 
        query or do an update based on it.
        
        In general a query object should be created via ``Session.query``, 
        not directly.
    '''
    def __get_query_result(self):
        return self.session.execute_query(self)
    def first(self):
        d = self.__get_query_result()
        def callback(result):
            for el in iter(result):
                return el
            return None            
        d.addCallback(callback)
        return d
    def all(self):
        def callback(result):
            return [obj for obj in iter(result)]
        d = self.__get_query_result()
        d.addCallback(callback)
        return d
        